package com.uchedao.seller.ui.main;

import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.uchedao.seller.R;
import com.uchedao.seller.ui.baidu.BaseFragForBaidu;
import com.uchedao.seller.ui.car.manage.CarManageFragment;
import com.uchedao.seller.ui.car.order.OrderManageFragment;
import com.uchedao.seller.ui.car.publish.CarPublishActivity;
import com.uchedao.seller.ui.user.center.UserCenterFragment;

import cn.jpush.android.api.JPushInterface;

/**
 * 主页
 * Created by zhengxizhen on 15/10/19.
 */
public class IndexFragment extends BaseFragForBaidu
        implements CompoundButton.OnCheckedChangeListener, View.OnClickListener {

    private static BaseFragForBaidu mInstance = null;

    /**
     * 获取单例
     */
    public static synchronized BaseFragForBaidu getInstance() {
        if (mInstance == null) {
            mInstance = new IndexFragment();
        }
        return mInstance;
    }


    @Override
    protected String getActivityTag() {
        return "IndexFragment";
    }

    @Override
    public int getViewLayoutId() {
        return R.layout.fragment_index;
    }

    @Override
    public void initView() {
        showToast(TAG);
        showToast(JPushInterface.getRegistrationID(getActivity()));
        bindOnCheckedChangeListener(R.id.tab_1);
        findViewById(R.id.tab_2).setOnClickListener(this);
        bindOnCheckedChangeListener(R.id.tab_3);
        bindOnCheckedChangeListener(R.id.tab_4);
        toChildFragment(CarManageFragment.getInstance());
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    public void bindOnCheckedChangeListener(int Id){
        ((RadioButton)findViewById(Id)).setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        showToast("前往子项片段:"+compoundButton.getId()+" checked<<<"+b+">>>>");
        if(b) {
            switch (compoundButton.getId()) {
                case R.id.tab_1:
                    toChildFragment(CarManageFragment.getInstance());
                    break;
                case R.id.tab_3:
                    toChildFragment(OrderManageFragment.getInstance());
                    break;
                case R.id.tab_4:
                    toChildFragment(UserCenterFragment.getInstance());
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.tab_2:
                startActivity(new Intent(getActivity(), CarPublishActivity.class));
                break;
        }
    }

    /**
     * 前往子项片段
     */
    private void toChildFragment(BaseFragForBaidu instance) {
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.index_fragment_content, instance, TAG);
        transaction.addToBackStack((String) null);
        transaction.commit();
    }
}
